<template>
  <el-dialog
    :visible.sync="visible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :title="title"
    custom-class="no-padding-dialog fields-dialog"
    append-to-body
    fullscreen
    lock-scroll>
    <fields-index
      ref="fieldsIndex"
      v-loading="loading"
      :props="fieldProps"
      :field-lib-fun="fieldLibFun"
      :get-list-promise="getListPromise"
      :save-promise="savePromise"
      @close="handleClose" />
  </el-dialog>
</template>

<script>
import FieldsIndex from '@/views/admin/fields/index'

export default {
  name: 'FieldsDialog',
  components: {
    FieldsIndex
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    rowData: {
      type: Object,
      default: () => {
        return {}
      }
    },
    rowIndex: Number
  },
  data() {
    return {
      loading: false
    }
  },
  computed: {
    title() {
      return `配置 阶段${this.rowIndex + 1} ${this.rowData.name} 表单`
    },
    fieldProps() {
      return {
        title: this.title
      }
    }
  },
  created() {},
  methods: {
    /**
     * @description: 获取字段
     * @param {*} list
     * @return {*}
     */
    fieldLibFun(list) {
      return list.filter(item => {
        return ![
          'pic',
          'detail_table',
          'field_group',
          'field_attention',
          'serial_number',
          'field_tag',
          'handwriting_sign',
          'rich_text_format'
        ].includes(item.formType)
      })
    },

    /**
     * @description: 获取数据字段
     * @return {*}
     */
    getListPromise() {
      return new Promise((resolve) => { // eslint-disable-line
        const fieldArr = this.rowData.formList || []
        // 一维字段数组数据
        const fieldData = []
        fieldArr.forEach(item => {
          item.forEach(subItem => {
            if (subItem) {
              const binaryStr = subItem.operating.toString(2).padStart(9, '0')
              // 如果数据开启必填，进行关闭
              if (binaryStr.charAt(5) == '1') {
                const strAry = binaryStr.split('')
                strAry[5] = '0'
                subItem.operating = strAry.join('')
              }
            }

            fieldData.push(subItem)
          })
        })

        resolve({ data: fieldArr })
      })
    },

    /**
     * @description: 保存字段
     * @param {*} list
     * @return {*}
     */
    savePromise(_, fieldList) {
      return new Promise((resolve) => { // eslint-disable-line
        this.$emit('change', fieldList)
        this.loading = false
        resolve()
        this.handleClose()
      })
    },

    /**
     * @description: 关闭弹窗
     * @return {*}
     */
    handleClose() {
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-dialog {
  .el-dialog__body {
    height: calc(100% - 50px);
  }
}
</style>
